<?php
/**
 * 后台用户组模型类
 *
 * @author liang<lang@vip.deyi.com>
 * @since   2016/11/29 18:04
 * @copyright  deyi.com
 */
namespace Apps\Acp\Models;

use Apps\Common\Model\BaseModel;

/**
 * Class ManagerGroup
 * @package Apps\Acp\Models
 */
class ManagerGroup extends BaseModel
{
    // 定义此model访问的数据库名
    protected static $_DBNAME = 'yyx_fanli';

    // 定义此model访问的表名
    protected static $_TABLENAME = 'acp_manager_group';

    public function getOneByGroupName($groupName)
    {
        $sql = 'SELECT * FROM ' . self::$_TABLENAME . ' WHERE groupname=? LIMIT 1';

        return $this->selectOne($sql, [$groupName]);
    }

    public function getGroupNamesByGids($gids)
    {
        if (empty($gids)) {
            return [];
        }
        $sql = 'SELECT id,groupname FROM ' . self::$_TABLENAME . ' WHERE id IN ' . $this->assemblePlaceholders($gids);
        $res = $this->selectCols($sql, ['id', 'groupname'], $gids);
        $data = [];

        foreach ($res as $v) {
            $data[$v->id] = $v->groupname;
        }

        return $data;
    }

    public function getListByConds($conditions, $offset, $limit)
    {
        list($values, $conds, $likeConds) = $this->assembleConditionsData($conditions);
        $where = $this->assembleComplexWhereSql($conds, $likeConds);

        $sql = 'SELECT * FROM ' . self::$_TABLENAME . $where . ' ORDER BY  dateline DESC ' . $this->assembleLimit($offset, $limit);

        return $this->select($sql, $values);
    }

    public function countListByConds($conditions)
    {
        list($values, $conds, $likeConds) = $this->assembleConditionsData($conditions);
        $where = $this->assembleComplexWhereSql($conds, $likeConds);

        $sql = 'SELECT COUNT(*) AS num FROM ' . self::$_TABLENAME . $where;

        return $this->selectCount($sql, $values);
    }
}
